import json
import time


# 读取JSON文件
def read_json_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
        return data


def read_json_file_two(file_path):
    with open(file_path, 'r') as file:
        data = json.load(file)
        return data


class VideoBean:
    id = 0
    name = ''
    url = ''
    e = ''
    i = ''
    tags = ''
    pUrl = ''
    vUrl = ''

    def __eq__(self, other):
        return self.pUrl == other.pUrl and self.vUrl == other.vUrl

    def __hash__(self):
        return hash(self.pUrl + self.vUrl)

    def __str__(self):
        return (f"VideoBean(\n"
                f"  id: {self.id},\n"
                f"  name: '{self.name}',\n"
                f"  url: '{self.url}',\n"
                f"  e: '{self.e}',\n"
                f"  i: '{self.i}',\n"
                f"  tags: '{self.tags}',\n"
                f"  pUrl: '{self.pUrl}',\n"
                f"  vUrl: '{self.vUrl}'\n"
                f")")


class SpiderInfo:
    # 爬虫的名称
    name = ''
    # 网址
    url = ''
    # 爬取下来的数据是否需要合并
    isMerge = False
    # 是否需要验证videoUrl的有效性
    isPlay = False

    def __init__(self, name, url, isMerge, isPlay):
        # 爬虫的名称
        self.name = name
        # 网址
        self.url = url
        # 爬取下来的数据是否需要合并
        self.isMerge = isMerge
        # 是否需要验证videoUrl的有效性
        self.isPlay = isPlay


# spiderInfo = SpiderInfo(name="ltaa", url="https://qtixdz.lkhsp2.motorcycles/cn/home/web/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ltab", url="https://159i.net/", isMerge=False, isPlay=False)

# spiderInfo = SpiderInfo(name="ydaa", url="https://kcnn.cc/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydab", url="https://hdawjqzet.buzz/", isMerge=False, isPlay=False)
# spiderInfo = SpiderInfo(name="ydac", url="https://fdkfcb.jpds4.work/cn/home/web/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydad", url="https://www.avnyg9.life/cn/home/web/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydae", url="https://301.sexav-301.com/ssss", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydaf", url="https://xiznsi.nmyy6.website/cn/home/web/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydag", url="https://wyswspb.cfd/", isMerge=False, isPlay=False)
# spiderInfo = SpiderInfo(name="ydah", url="https://avfolder.com/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydai", url="https://xt.xintangshipin.icu/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydaj", url="https://rqppjc.lkhsp3.ink/cn/home/web/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydak", url="https://159i.net/", isMerge=False, isPlay=False)
# spiderInfo = SpiderInfo(name="ydal", url="https://179na.com/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydam", url="https://www.cztv1017.sbs/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydan", url="https://jingpinbd-006.jingpyn.sbs/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydao", url="https://www.avdg.xyz/", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydap", url="https://77maott.com/index.html", isMerge=True, isPlay=False)
# spiderInfo = SpiderInfo(name="ydaq", url="https://992kp10.630av.work/index.html", isMerge=False, isPlay=False)
spiderInfo = SpiderInfo(name="ydar", url="https://wbka--se8feirae.lewhhhzzz7y6m1.xyz/xsp", isMerge=True, isPlay=False)


def get_message(name):
    jsonStr = read_json_file(f"/home/ccg/1/{name}.json")
    videoList = []
    for i in jsonStr:
        video = VideoBean()
        video.id = 0
        video.name = i['name']
        video.url = i['url']
        video.tags = i['tags']
        video.pUrl = i['pUrl']
        video.vUrl = i['vUrl']
        videoList.append(video)
    jsonStr.clear()
    return videoList


# 1完整数据并去重复
if __name__ == '__main__':
    # with open('/home/ccg/1/1.json', 'r', encoding='utf-8') as file:
    #     str = file.read()
    # # 写入字符串到文件
    # with open('/home/ccg/1/5.json', 'w', encoding='utf-8') as file:
    #     file.write(str[4:])

    print('开始')
    timestamp = int(time.time())
    result = []
    for i in range(1, 5):
        print(i)
        m_list = get_message(i)
        result.extend(m_list)
        print(len(result))
    print(len(result))
    test_list = list(set(result))
    print("原始数据去重后一共: " + str(len(test_list)))
    if spiderInfo.isMerge:
        print("需要合并")
        # 对数据进行分组，一组视频地址，一组缩略图
        listVideo = []
        listPic = []
        for i in test_list:
            if len(i.pUrl) == 0 and len(i.vUrl) > 0:
                listVideo.append(i)
            elif len(i.vUrl) == 0 and len(i.pUrl) > 0:
                listPic.append(i)
        test_list.clear()
        print("视频地址一共: " + str(len(listVideo)))
        print("图片地址一共: " + str(len(listPic)))
        for index, videoData in enumerate(listVideo):
            if index % 100 == 0:
                print(f"处理到第 {index} 条数据")
            for pictureUrlData in listPic:
                if videoData.url == pictureUrlData.url:
                    videoData.pUrl = pictureUrlData.pUrl
        listPic.clear()
        for num in reversed(listVideo):
            if len(num.pUrl) == 0:
                listVideo.remove(num)
        test_list = list(set(listVideo))
    videoFinalList = []
    print("视频地址一共: " + str(len(test_list)))
    for i in test_list:
        if '>' in i.vUrl or ')' in i.vUrl or '"' in i.vUrl:
            print(i.vUrl)
        tinydict = {'id': 0, 'name': i.name, 'url': '', 'tags': i.tags, 'pUrl': i.pUrl, 'vUrl': i.vUrl.replace("https://", "http://")}
        videoFinalList.append(tinydict)
    test_list.clear()
    # 将JSON数组转换为字符串
    json_string = json.dumps(videoFinalList, ensure_ascii=False)
    # 将字符串写入txt文件
    with open('../../../' + spiderInfo.name + '1.json', 'w', encoding='utf-8') as file:
        file.write(json_string)
    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')
